//
//  XXTool.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/2.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XXTool.h"
#import "SideBarViewController.h"
#import "MainViewController.h"
#import "indexViewController.h"

@interface XXTool ()<SideBarViewControllerDelegate>

@end

@implementation XXTool

+(NSString*)dataBasePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [paths objectAtIndex:0];
    NSString *database_path = [documents stringByAppendingPathComponent:@"LocalExhibitorData.realm"];
    return database_path;
}

+(void)networkError
{
    [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"网络连接错误", nil)];
}

+(BOOL)validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

+(NSString*)timeToStamp:(NSString*)time
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    
    NSDate *datenow = [formatter dateFromString:time];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    return timeSp;
}


+(NSString*)stampToTime:(NSString*)stamp
{
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:[stamp doubleValue]];
    NSDateFormatter * df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSString *regStr = [df stringFromDate:date];
    return regStr;
}

+(NSString*)stampToTimeWithoutYear:(NSString *)stamp
{
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:[stamp doubleValue]];
    NSDateFormatter * df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"MM-dd HH:mm"];
    NSString *regStr = [df stringFromDate:date];
    return regStr;
}

+(CGFloat)getTextHeightByText:(NSString*)text Font:(UIFont*)font Width:(CGFloat)width
{
    if([text isKindOfClass:[NSNull class]] || text.length==0)
        return 0;
    CGFloat height=0;
    
    NSDictionary *attributes = @{NSFontAttributeName: font};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attributes
                                     context:nil];
    height=rect.size.height;
    return height+5;
}

+(CGFloat)getTextWidthByText:(NSString*)text Font:(UIFont*)font
{
    if([text isKindOfClass:[NSNull class]] || text.length==0)
        return 0;
    CGFloat height=0;
    
    
    NSDictionary *attributes = @{NSFontAttributeName: font};
    if([text characterAtIndex:0]=='\n')
    {
        text=[NSString stringWithFormat:@" %@",text];
    }
    CGRect rect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attributes
                                     context:nil];
    height=rect.size.width;
    return height+5;
}

+(UIColor *)colorWithHexString:(NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

+(UIViewController*)getViewController:(UIView*)oView
{
    for(UIView* view=oView;view;view=[view superview])
    {
        UIResponder* responder=[view nextResponder];
        if([responder isKindOfClass:[UIViewController class]])
        {
            return (UIViewController*)responder;
        }
    }
    return nil;
}

+(NSString*)formatStringWithLanguage:(NSString *)needFormatString
{
    NSString *symbol = @"";
    if ([needFormatString isEqualToString:@""])
    {
        symbol = @"";
    }
    else
    {
        symbol = @"&";
    }
    NSString* lang = @"";
    if ([XXTool didCurrentLanguageChinese])
    {
        lang = @"zh";
    }
    else
    {
        lang = @"en";
    }
    return [NSString stringWithFormat:@"%@%@lang=%@",needFormatString,symbol,lang];
}

+(UIColor*)colorWithProject
{
//    return [UIColor colorWithRed:19/255.0 green:145/255.0 blue:78/255.0 alpha:1.0];
    return [UIColor colorWithRed:17/255.0 green:155/255.0 blue:86/255.0 alpha:1.0];
}

+(UIColor*)colorProjectWithAlpha:(CGFloat)alpha
{
    //    return [UIColor colorWithRed:19/255.0 green:145/255.0 blue:78/255.0 alpha:1.0];
    return [UIColor colorWithRed:17/255.0 green:155/255.0 blue:86/255.0 alpha:alpha];
}

/**
 * 发送验证码后灰色字体
 */
+(UIColor*)colorWithHuiYzm{
    return [UIColor colorWithRed:158/255.0 green:159/255.0 blue:160/255.0 alpha:1.0];
}

+(UIColor*)colorWithBar
{
    return [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];
}

+(UIColor*)colorWithLine
{
    return [UIColor colorWithRed:203/255.0 green:203/255.0 blue:203/255.0 alpha:1.0];
}

+(UIColor*)colorWithBackGround
{
    return [UIColor colorWithRed:246/255.0 green:247/255.0 blue:248/255.0 alpha:1.0];
}

+(UIFont*)boldSystemFontOfSize:(CGFloat)size
{
    //  return [UIFont fontWithName:@"MFJinHei_Noncommercial-Regular" size:size];
    return [UIFont boldSystemFontOfSize:size];
    // return [UIFont fontWithName:@"STHeitiSC-Medium" size:size];
}

+(UIFont*)systemFontOfSize:(CGFloat)size
{
    // return [UIFont fontWithName:@"MFJinHei_Noncommercial-Regular" size:size];
    return [UIFont fontWithName:@"Helvetica" size:size];
    //  return [UIFont fontWithName:@"AppleSDGothicNeo-Bold" size:size];
    //return [UIFont fontWithName:@"STHeitiSC-Light" size:size];
}

+(float)getWidthWithScreenWidth:(float)width
{
    return (float)width*[UIScreen mainScreen].bounds.size.width/750;
}

+(float)getHeightWithScreenHeight:(float)height
{
    return (float)height*[UIScreen mainScreen].bounds.size.height/1334;
}

+(BOOL)didCurrentLanguageChinese
{
    NSArray *languages = [NSLocale preferredLanguages];
    NSString *currentLanguage = [languages objectAtIndex:0];
    if([currentLanguage isEqualToString:@"zh-Hans-CN"]||[currentLanguage isEqualToString:@"zh-Hans-US"]||[currentLanguage isEqualToString:@"zh-Hans"]||[currentLanguage isEqualToString:@"zh-Hant-US"]||[currentLanguage isEqualToString:@"zh-Hant-CN"]||[currentLanguage isEqualToString:@"zh-TW"]||[currentLanguage isEqualToString:@"zh-HK"])
    {
        currentLanguage = @"zh-Hans";
    }
    if ([currentLanguage isEqualToString:@"zh-Hans"])
    {
        return YES;
    }
    return NO;
}

//生成纯色图片
+(UIImage*) createImageWithColor:(UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@end
